package com.gfdd.tea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.gfdd.tea.common.R;
import com.gfdd.tea.dto.AssessDto;
import com.gfdd.tea.pojo.Assess;
import com.gfdd.tea.pojo.User;
import com.gfdd.tea.service.AssessService;
import com.gfdd.tea.utlis.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/assess")
public class AssessController {
    @Autowired
    private AssessService assessService;
    @PostMapping("/save")
    public R<String> saveAssess(@RequestBody Assess assess,Long userId){
        assess.setCreateTime(DateUtils.DateFormat(new Date()));
        assessService.save(assess);
        return R.success("保存成功");
    }

    /**
     * 返回对应的茶的评价信息
     * @param page
     * @param pageSize
     * @param id
     * @return
     */
    @GetMapping("/page")
    public R<Page<AssessDto>> pageInfo(Long page, Long pageSize, Long id){
        Page<Assess> pageInfo=new Page<>(page,pageSize);
        Page<AssessDto> pageReturn=new Page<>();
        LambdaQueryWrapper<Assess> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Assess::getTeaId,id);
        wrapper.orderByDesc(Assess::getCreateTime);
        assessService.page(pageInfo,wrapper);
        BeanUtils.copyProperties(pageInfo,pageReturn,"records");
        List<Assess> records = pageInfo.getRecords();
        List<AssessDto> assessList = records.stream().map((item -> {
            AssessDto dto = new AssessDto();
            BeanUtils.copyProperties(item, dto);
            User assessUser = assessService.queryAssessUserById(item.getAssessUid());
            dto.setAssessBy(assessUser.getNickName());
            dto.setUserImage(assessUser.getAvatarUrl());
            return dto;
        })).collect(Collectors.toList());
        pageReturn.setRecords(assessList);
        return R.success(pageReturn);
    }

}
